﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:pe_stzj
	/// </summary>
	public partial class pe_stzj
	{
		public pe_stzj()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long xh)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from pe_stzj");
			strSql.Append(" where xh=@xh");
			SqlParameter[] parameters = {
					new SqlParameter("@xh", SqlDbType.BigInt)
			};
			parameters[0].Value = xh;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(Model.pe_stzj model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into pe_stzj(");
			strSql.Append("stlx,stms,couTiSuPd,couTiSuXz,curPd,curXz,csn,addDateTime)");
			strSql.Append(" values (");
			strSql.Append("@stlx,@stms,@couTiSuPd,@couTiSuXz,@curPd,@curXz,@csn,@addDateTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@stlx", SqlDbType.Int,4),
					new SqlParameter("@stms", SqlDbType.NVarChar,200),
					new SqlParameter("@couTiSuPd", SqlDbType.Int,4),
					new SqlParameter("@couTiSuXz", SqlDbType.Int,4),
					new SqlParameter("@curPd", SqlDbType.Int,4),
					new SqlParameter("@curXz", SqlDbType.Int,4),
					new SqlParameter("@csn", SqlDbType.VarChar,50),
					new SqlParameter("@addDateTime", SqlDbType.DateTime)};
			parameters[0].Value = model.stlx;
			parameters[1].Value = model.stms;
			parameters[2].Value = model.couTiSuPd;
			parameters[3].Value = model.couTiSuXz;
			parameters[4].Value = model.curPd;
			parameters[5].Value = model.curXz;
			parameters[6].Value = model.csn;
			parameters[7].Value = model.addDateTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.pe_stzj model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update pe_stzj set ");
			strSql.Append("stlx=@stlx,");
			strSql.Append("stms=@stms,");
			strSql.Append("couTiSuPd=@couTiSuPd,");
			strSql.Append("couTiSuXz=@couTiSuXz,");
			strSql.Append("curPd=@curPd,");
			strSql.Append("curXz=@curXz,");
			strSql.Append("csn=@csn,");
			strSql.Append("addDateTime=@addDateTime");
			strSql.Append(" where xh=@xh");
			SqlParameter[] parameters = {
					new SqlParameter("@stlx", SqlDbType.Int,4),
					new SqlParameter("@stms", SqlDbType.NVarChar,200),
					new SqlParameter("@couTiSuPd", SqlDbType.Int,4),
					new SqlParameter("@couTiSuXz", SqlDbType.Int,4),
					new SqlParameter("@curPd", SqlDbType.Int,4),
					new SqlParameter("@curXz", SqlDbType.Int,4),
					new SqlParameter("@csn", SqlDbType.VarChar,50),
					new SqlParameter("@addDateTime", SqlDbType.DateTime),
					new SqlParameter("@xh", SqlDbType.BigInt,8)};
			parameters[0].Value = model.stlx;
			parameters[1].Value = model.stms;
			parameters[2].Value = model.couTiSuPd;
			parameters[3].Value = model.couTiSuXz;
			parameters[4].Value = model.curPd;
			parameters[5].Value = model.curXz;
			parameters[6].Value = model.csn;
			parameters[7].Value = model.addDateTime;
			parameters[8].Value = model.xh;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long xh)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pe_stzj ");
			strSql.Append(" where xh=@xh");
			SqlParameter[] parameters = {
					new SqlParameter("@xh", SqlDbType.BigInt)
			};
			parameters[0].Value = xh;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string xhlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from pe_stzj ");
			strSql.Append(" where xh in ("+xhlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.pe_stzj GetModel(long xh)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 xh,stlx,stms,couTiSuPd,couTiSuXz,curPd,curXz,csn,addDateTime from pe_stzj ");
			strSql.Append(" where xh=@xh");
			SqlParameter[] parameters = {
					new SqlParameter("@xh", SqlDbType.BigInt)
			};
			parameters[0].Value = xh;

			Model.pe_stzj model=new Model.pe_stzj();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.pe_stzj DataRowToModel(DataRow row)
		{
			Model.pe_stzj model=new Model.pe_stzj();
			if (row != null)
			{
				if(row["xh"]!=null && row["xh"].ToString()!="")
				{
					model.xh=long.Parse(row["xh"].ToString());
				}
				if(row["stlx"]!=null && row["stlx"].ToString()!="")
				{
					model.stlx=int.Parse(row["stlx"].ToString());
				}
				if(row["stms"]!=null)
				{
					model.stms=row["stms"].ToString();
				}
				if(row["couTiSuPd"]!=null && row["couTiSuPd"].ToString()!="")
				{
					model.couTiSuPd=int.Parse(row["couTiSuPd"].ToString());
				}
				if(row["couTiSuXz"]!=null && row["couTiSuXz"].ToString()!="")
				{
					model.couTiSuXz=int.Parse(row["couTiSuXz"].ToString());
				}
				if(row["curPd"]!=null && row["curPd"].ToString()!="")
				{
					model.curPd=int.Parse(row["curPd"].ToString());
				}
				if(row["curXz"]!=null && row["curXz"].ToString()!="")
				{
					model.curXz=int.Parse(row["curXz"].ToString());
				}
				if(row["csn"]!=null)
				{
					model.csn=row["csn"].ToString();
				}
				if(row["addDateTime"]!=null && row["addDateTime"].ToString()!="")
				{
					model.addDateTime=DateTime.Parse(row["addDateTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select xh,stlx,stms,couTiSuPd,couTiSuXz,curPd,curXz,csn,addDateTime ");
			strSql.Append(" FROM pe_stzj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" xh,stlx,stms,couTiSuPd,couTiSuXz,curPd,curXz,csn,addDateTime ");
			strSql.Append(" FROM pe_stzj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM pe_stzj ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.xh desc");
			}
			strSql.Append(")AS Row, T.*  from pe_stzj T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "pe_stzj";
			parameters[1].Value = "xh";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

